package quatrecentvingtetun;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by IntelliJ IDEA.
 * User: seeme
 * Date: 23 mars 2009
 * Time: 10:43:03
 * To change this template use File | Settings | File Templates.
 */
public class Joueur implements java.lang.Comparable{
    private int jetons;
    private String nom;
    private Figure figure;
    private int nbRelance;

    public void setNbRelance(int n){
        nbRelance = n;
    }

    public int getNbRelance(){
        return nbRelance;
    }

    public Joueur(String nom){
                     this.nom=nom;
                     figure=new Figure();
    }

    public Joueur(String nom, int nb_jetons){
                     jetons=nb_jetons;
                     this.nom=nom;
                     figure=new Figure();
    }

    public int lancerDe(){
        return Hasard.entier(6)+1;        
    }

    public void lancer(){
        figure = new Figure(TriEntier(lancerDe(),lancerDe(),lancerDe()));
    }

    public void lancerDecharge(Figure in, String locked){
        char a, b, c;
        a = locked.charAt(0);
        b = locked.charAt(1);
        c = locked.charAt(2);

        int d, e, f;
        d = Character.getNumericValue(in.toString().charAt(0));
        e = Character.getNumericValue(in.toString().charAt(1));
        f = Character.getNumericValue(in.toString().charAt(2));

        figure = new Figure(TriEntier(a == 'X'? d : lancerDe(), b == 'X'? e : lancerDe(), c == 'X'? f : lancerDe()));
    }

    public void setJetons(int n){
        jetons = n;
    }

    public int getJetons(){
        return jetons;
    }

    public void setNom(String s){
        nom = s;
    }

    public String getNom(){
        return nom;
    }

    public void setFigure(Figure f){
        figure = f;
    }

    public Figure getFigure(){
        return figure;
    }

    public Joueur donneJetons( Joueur j){
        if(jetons < figure.getScore()){
            j.setJetons(j.getJetons()+jetons);
            jetons = 0;
        }else{
            jetons -= figure.getScore();
            j.setJetons(j.getJetons()+figure.getScore());
        }
       return j;
    }

    public String TriEntier(int n1, int n2, int n3) {
        ArrayList atrier=new ArrayList();
        atrier.add(n2);
        atrier.add(n1);
        atrier.add(n3);
        Collections.sort(atrier,Collections.reverseOrder());
       return atrier.get(0).toString()+atrier.get(1).toString()+atrier.get(2).toString();

    }


    public int compareTo(Object autre){
      int nombre1 = ((Joueur) autre).getFigure().getScore();
      int nombre2 = figure.getScore();
      if (nombre1 > nombre2)  return -1;
      else if(nombre1 == nombre2) return 0;
      else return 1;
    }


}